package android.tusnotas.notification;

import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.tusnotas.activities.R;
import android.tusnotas.activities.ShowCourse;
import android.tusnotas.activities.ShowInstancesActivity;
import android.tusnotas.activities.extras.ShowCourseActivityExtras;
import android.tusnotas.changes.Change;
import android.tusnotas.changes.PushChange;

public class NewPushNotification extends TusNotasNotification{

	private static Integer id = NotificationIDs.NEW_PUSH_NOTIFICATION;

	private Change change = null;

	public NewPushNotification(Change change){
		super(R.drawable.tusnotas, change.getBarTitle(), System.currentTimeMillis());
		this.change = change;

	}

	@Override
	public void configureNotificacion(Service service) {
		Context context = service.getApplicationContext();

		CharSequence title = change.getTitle();
		CharSequence content = change.getContent();

		Intent notificationIntent = null;
		try {
			notificationIntent = new Intent(service.getApplicationContext(), ShowCourse.class);
			notificationIntent.putExtra(ShowCourseActivityExtras.COURSE_ID, ((PushChange) change).getCourseId());
		} catch (Exception e) {
			// cant find course, show instances
			notificationIntent = new Intent(service.getApplicationContext(), ShowInstancesActivity.class);
			e.printStackTrace();
		}
		notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent contentIntent = PendingIntent.getActivity(service.getApplicationContext(), (int) System.currentTimeMillis(), notificationIntent, 0);
		this.setLatestEventInfo(context, title, content, contentIntent);		
	}

	@Override
	public Integer getId() {
		return id++;
	}

}
